package com.zb.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //web服务启动的时候，过滤器就初始化了，随时等待请求过来！
        System.out.println("CharacterEncodingFilter初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        System.out.println("CharacterEncodingFilter执行前........");
        //让请求继续向下执行，不写的话请求到这里就截止了
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("CharacterEncodingFilter执行后........");
    }

    @Override
    public void destroy() {
        //web服务器关闭的时候，过滤器才会销毁
        System.out.println("CharacterEncodingFilter销毁");
    }
}
